/**
 * @author zhaozhi3758
 */
listView = function() {
	var store, grid, win, iframe;
	var url = cfg.cxt + "/example/novel/info.sj?id=";
	var sm =  new Ext.grid.CheckboxSelectionModel({ singleSelect: false });
	var fields = [
	       "id",
	       "author",
	       "codeName",
	       "title",
	       "recommend",
	       "writeState",
	       "isVolume",
	       "createTime"
	];
	return {
		init : function() {
			store = this.createStore();
			store.on('beforeload', function() {
				this.baseParams = {
				     "title" : Ext.getCmp('qtitle').getValue()
				};
			});
			
			
			grid = this.createGrid();
			
			
			new Ext.Viewport({ 
			      layout : 'fit',
				  items : [grid]
			});
			
			
			
			//this.queryResItem();
		},
		
		createStore : function() {
		    return new ExtStore({
					     url : '/example/novel/list.sj',
					     fields : fields
				   });
		},
		
		createGrid : function() {
			return new ExtGrid({
						store : store,
						title : '<div class="panelTitleCss">小说管理信息表</div>',
						sm : sm  ,
						columnLines:true,
						columns : [
					        new Ext.grid.RowNumberer(), 
					        sm ,
					        { sortable : true,dataIndex : "title",header : "小说名称"},
						    { sortable : true,dataIndex : "author",width:30,header : "作者" },
						    { sortable : true,dataIndex : "recommend",width:40,align:'center',header : "推荐状态",
						      renderer:function(v, c, rd, ri, ci, s){
						    		if(v == 0 ) {
						    			return "不推荐";
						    		} else if(v == 1 ){
						    			return "编辑推荐";
						    		} else if(v == 2 ){
						    			return "潜 力 榜";
						    		} else if(v == 3 ){
						    			return "封面推荐";
						    		}
						    	} 
						    },
						    { sortable : true,dataIndex : "writeState",width:40,header : "连载状态",align:'center',
					    	  renderer:function(v, c, rd, ri, ci, s){
					    		  return  v ? '<span style="color:red;font-weight:bold;">全文完</span>' :  
					    			          '<span style="color:green;font-weight:bold;">连载中</span>';
						    	} 
						    },
						    { sortable : true,dataIndex : "createTime",header : "创建日期" ,align:'center',width:50},
						    { sortable : true,dataIndex : "id",width:80,align:'center',header : "操作",
						      renderer:function(v, c, rd, ri, ci, s){
						    	    var s = '<a href="javascript:listView.show('+v+');">查看</a>';
						    		return s;
						    	} 
						    
						    }
						],
						tbar : listView.createTbar(),
						bbar : {
						    id : 'page',
							xtype : 'pageinfobar',
							pageSize : 30,
							store : store,
							pageMethod : listView.queryResItem
						}
					});
		},
		
		createTbar : function() {
			return [{
				text : '新增',
				iconCls : 'page_addIcon',
				handler : function() {
					listView.addInit();
				}
			},'-',{
				text : '修改',
				iconCls : 'page_edit_1Icon',
				handler : function() {
					listView.editInit();
				}
			},'-',{
				text : '删除',
				iconCls : 'page_delIcon',
				handler : function() {
					listView.deleteItem();
				}
			},'-','->',{
				xtype : "label",
				text : "小说名称："
			}, {
				xtype:"textfield",
				width:100,
				id:'qtitle',
				value : ""
		    },'-',{
				text : '查询',
				iconCls : 'previewIcon',
				handler : function() {
					listView.queryResItem();
				}
			},'-',{
				text : '刷新',
				iconCls : 'arrow_refreshIcon',
				handler : function() {
					window.location.reload();
				}
			} ];
		},
		
		queryResItem : function(v) {
			var ptool = Ext.getCmp('page');
			if ( !v ){
			   v = ptool.pageSize;
			}
			store.load({
				params : {
					"start" : (ptool.getPageData().activePage -1 ) * v,
					"limit" : v
				}
		    });
		},
		
		addInit : function() {
			this.onAddOrEditInit('');
			win.show();
			win.setTitle('新增小说');
		},
		
		editInit : function() {
			var record = grid.getSelectionModel().getSelected();
			if (Ext.isEmpty(record)) {
				Ext.Mytip.msg('提示', '请先选择要修改的记录!');
				return;
			}
			this.onAddOrEditInit(record.data.id);
			win.show();
			win.setTitle('修改小说');
		},
		
		show : function(id){
			this.onAddOrEditInit(id + '&show=true');
			win.show();
			win.setTitle('查看小说');
		},
		
		onAddOrEditInit : function(id){
			if(!win){
				win = this.createWindow(id);
			} else {
				document.getElementById("iframe").src  = url + id;
			}
		},
		
	    subForm : function() {
	    	$('#subBtn', iframe.document).click();
		},
		
		tip : function(text){
			 Ext.Mytip.msg('提示', text);
		},
		
		closeWindow : function(){
			win.hide();
		},
		
		createWindow : function(id){
			return new ExtWin({
				    width : 600,
					height : 400,
					items : [{
						xtype : 'panel',
						html  : '<iframe id="iframe" src=' + url + id + ' scrolling="auto" frameborder="0" width="100%" height="100%"></iframe>'
					}],
					buttons : [{
						text : '保存',
						iconCls : 'acceptIcon',
						handler : function() {
							iframe = document.getElementById("iframe").contentWindow;
							listView.subForm();
						}
					}, {
						text : '关闭',
						iconCls : 'deleteIcon',
						handler : function() {
							win.hide();
						}
					}]
				});
		},
		
		deleteItem : function() {
			var sm = grid.getSelectionModel();
			if(sm.getCount() == 0){
				Ext.Mytip.msg('提示', '您尚未选择要删除的记记录！');
				return;
			}
			var ids = [];
			sm.each(function(r){
			   ids.push(r.data.id);
			});
			
			Ext.Msg
			   .confirm(
					'请确认',
					'<span style="color:red"><b>提示：</b>确定删除吗？</span>',
					function(btn, text) {
						if (btn == 'yes') {
							Ext.Ajax.request({
								url : cfg.cxt + '/example/novel/delete.sj',
								success : function(response) {
									var rt = Ext.decode(response.responseText);
									if (rt.success){
										listView.queryResItem();
									    Ext.Mytip.msg('提示', '删除成功！');
									} else {
										safetys.ajaxError(response);
									}
								},
								failure : safetys.ajaxError,
								params : {
									"ids" : ids.join(',')
								}
							});
						}
				});
			
		}

	}
}();
Ext.onReady(listView.init, listView);